import {
  userAccountLoginAPI,
  userMobileLoginAPI,
  userQQPatchLoginAPI
} from '@/api/user'
// import xtxMessage from '@/components/library/Message'

// 重置用户数据
const resetProfile = () => ({
  id: '',
  avatar: '',
  nickname: '',
  account: '',
  mobile: '',
  token: ''
})

const state = () => {
  return {
    // 用户信息
    profile: resetProfile()
  }
}

const mutations = {
  // 保存用户信息
  SET_PROFILE (state, profile) {
    // Object.keys(profile).map(key => (state.profile[key] = profile[key]))
    state.profile = profile
  },
  // 重置用户信息
  CLEAR_PROFILE (state) {
    state.profile = resetProfile()
  }
}
const actions = {
  // 账号密码登录
  async accountLogin ({ commit }, data) {
    // console.log('登录')
    const res = await userAccountLoginAPI(data)
    commit('SET_PROFILE', res)
    // console.log('完成登录')
  },
  // 手机短信登录
  async mobileLogin ({ commit }, data) {
    const res = await userMobileLoginAPI(data)
    commit('SET_PROFILE', res)
  },
  async QQLogin ({ commit }, data) {
    const res = await userQQPatchLoginAPI(data)
    commit('SET_PROFILE', res)
  },
  // 退出登录
  logout ({ commit }) {
    commit('CLEAR_PROFILE')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
